#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ssm.h"


void printDynamic(Dynamic source)
{
	printf("D_a		= %f aerodynamic drag (N)\n", source.D_a);
	printf("P_a		= %f aerodynamic power (W)\n", source.P_a);
	printf("E_a		= %f aerodynamic energy (J)\n", source.E_a);
	printf("D_rr		= %f rolling resistance (N)\n", source.D_rr);
	printf("P_rr		= %f rolling power (W)\n", source.P_rr);
	printf("E_rr		= %f rolling energy (J)\n", source.E_rr);
	printf("B_charge	= %f Battery charge used so far, between 0.0 and 1.0 (W S)\n", source.B_charge);
	printf("P_array		= %f array power (W)\n", source.P_array);
	printf("E_array		= %f array energy (W S)\n", source.E_array);
	printf("P_p		= %f parasitic power losses (W)\n", source.P_p);
	printf("E_p		= %f parasitic energy losses (W S)\n", source.E_p);
	printf("E_charge	= %f Battery charge, between 0,0 and 1.0\n", source.E_charge);
	printf("F_g		= %f Force of gravity, in direction of travel (N)\n", source.F_g);
	printf("P_g		= %f Power from change in gravational potential energy. (W)\n", source.P_g);
	printf("E_g		= %f Energy from change in gravational potential energy. (J)\n", source.E_g);
	printf("P[3]		= %f,%f,%f\n", source.P[0], source.P[1], source.P[2]);
	printf("D[3]		= %f,%f,%f\n", source.D[0], source.D[1], source.D[2]);
	printf("distance	= %f distance travelled (M)\n", source.distance);
	printf("speed		= %f speed (M/S)\n", source.speed);
	printf("a		= %f accelleration (M / S^2)\n", source.a);
	printf("latitude	= %f gps position\n", source.latitude);
	printf("longitude	= %f gps position\n", source.longitude);
	printf("h		= %f altitude (M)\n", source.h);
}

int writeDynamic(char* filename, Dynamic *source)
{
	FILE *fp;
	if((fp=fopen(filename, "wb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for output\n", filename);
  		exit(1);
	}
	if((fwrite(source, sizeof(Dynamic), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}

int readDynamic(char* filename, Dynamic *dest)
{
	FILE *fp;
	if((fp=fopen(filename,"rb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for input\n", filename);
  		exit(1);
	}
	if((fread(dest, sizeof(Dynamic), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}
